﻿<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title></title>
	
<style type="text/css">
ul {
	list-style:none;
}

ul li ul {
	list-style:none;
	display:inline;
	margin:0px;
	padding:0px;
}
ul li ul li {
	margin-left:5px;
	display:inline;
}

td {
	border-right: 1px solid #C1DAD7;
	border-bottom: 1px solid #C1DAD7;
	font-size:11px;
	padding: 6px 6px 6px 12px;
} 

</style>

</head>
<body>
    <div>
        <fieldset>
            <div style="text-align: center;">
                ONU文件&nbsp;&nbsp;:&nbsp;&nbsp;<input id="primaryKeyFile" type="file" size="55" />
                BRAS文件&nbsp;&nbsp;:&nbsp;&nbsp;<input id="foreingKeyFile" type="file" size="55" />
                &nbsp;&nbsp;<input id="btnImport" type="button" value="加载数据" onclick="loadExcel()" />
            </div>
        </fieldset>
        <fieldset>
            <div style="text-align: center;">
                用户名&nbsp;:&nbsp;<input id="tbxUsername" type="text" size="15"/>
                业务VLAN&nbsp;:&nbsp;<input id="tbxBusinessVLAN" type="text"  size="10"/>
                外层VLAN&nbsp;:&nbsp;<input id="tbxOuterLayerVLAN" type="text"  size="10"/>
                业务VLAN(始)&nbsp;:&nbsp;<input id="tbxBeginBusinessVLAN" type="text"  size="10"/>
                业务VLAN(终)&nbsp;:&nbsp;<input id="tbxEndBusinessVLAN" type="text"  size="10"/>
                &nbsp;&nbsp;<input id="btnSearch" type="button" value="查询" onclick="searchFromMergedResultArray()" /> <!--onclick="searchFromMergedResultArray()" /> -->
                &nbsp;&nbsp;<input id="btnExport" type="button" value="导出查询结果" onclick="exportSearchResultToExcel()"/>
            </div>
        </fieldset>		
		<div id="loadMsg" style="color:red; font-weight:bold; text-align:center;"></div>
		<div id="htmlContent"></div>
    </div>
    <script language="javascript" type="text/javascript">
		var activeXObject = null;
		var workBooks = null;

        var pkSheet = null;
        var pkArray = [];
        var pkRowCount = 0;
        var pkColCount = 0;

        var fkSheet = null;
        var fkArray = [];
        var fkRowCount = 0;
        var fkColCount = 0;

        var mergedResultArray = [];
		var outputSheet = null;
		var idTmr = null;
		
		function setMessage(msg) {
			document.getElementById("loadMsg").innerHTML = msg;
		}
		
		function disabledButtons(isDisabled) {
			document.getElementById("btnImport").disabled = isDisabled;
			document.getElementById("btnSearch").disabled = isDisabled;
			document.getElementById("btnExport").disabled = isDisabled;
		}
		
        function loadExcel() {
            var pkFilename = document.getElementById("primaryKeyFile").value;
            var fkFilename = document.getElementById("foreingKeyFile").value;
			
			disabledButtons(true);
			setMessage("正在加载数据,请稍等...");

            initWorksheets(pkFilename, fkFilename);
            convertDataToObjectArray();
            mergeByBusinessVLAN();
			
			disabledButtons(false);
			setMessage("数据加载成功！！！");
        }

        function initWorksheets(pkFilename, fkFilename) {    
            // Init ActiveXObject control
            try {
                activeXObject = new ActiveXObject("Excel.Application");

            } catch (ex) {
                setMessage("Excel对象无法创建，请开启浏览器的ActiveX控键限制，并安装Excel2003！");
            }

            try {
                //load data from primary file5
                workBooks = activeXObject.Workbooks.open(pkFilename);
                pkSheet = workBooks.ActiveSheet;
                pkRowCount = workBooks.Worksheets(1).UsedRange.Cells.Rows.Count;
                pkColCount = workBooks.Worksheets(1).UsedRange.Columns.Count;

                //load data from foreing file
                workBooks = activeXObject.Workbooks.open(fkFilename);
                fkSheet = workBooks.ActiveSheet;
                fkRowCount = workBooks.Worksheets(1).UsedRange.Cells.Rows.Count;
                fkColCount = workBooks.Worksheets(1).UsedRange.Columns.Count;

            } catch (ex) {
                setMessage("加载数据失败，请检查文件类型，确保是Excel文件");
            }
        }

        function convertDataToObjectArray() {
            //Convert excel data to ONU object
            var startStep = 2;
            for (var rowIndex = startStep; rowIndex <= pkRowCount; rowIndex++) {
			
                var pkObj = new onuObj(pkSheet.Cells(rowIndex, 1).value,
    					pkSheet.Cells(rowIndex, 2).value,
    					pkSheet.Cells(rowIndex, 3).value,
    					pkSheet.Cells(rowIndex, 4).value,
    					pkSheet.Cells(rowIndex, 5).value,
    					pkSheet.Cells(rowIndex, 6).value,
    					pkSheet.Cells(rowIndex, 7).value,
    					pkSheet.Cells(rowIndex, 8).value,
    					pkSheet.Cells(rowIndex, 9).value,
    					pkSheet.Cells(rowIndex, 10).value,
    					pkSheet.Cells(rowIndex, 11).value,
    					pkSheet.Cells(rowIndex, 12).value,
						pkSheet.Cells(rowIndex, 13).value);

                pkArray[rowIndex - startStep] = pkObj;
            }

            //Convert excel data to BRAS object
            for (var rowIndex = startStep; rowIndex <= fkRowCount; rowIndex++) {

                var fkObj = new brasObj(fkSheet.Cells(rowIndex, 1).value,
    					fkSheet.Cells(rowIndex, 2).value,
    					fkSheet.Cells(rowIndex, 3).value,
    					fkSheet.Cells(rowIndex, 4).value);

                fkArray[rowIndex - startStep] = fkObj;
            }  
        }

        function mergeByBusinessVLAN() {
            var resultIndex = 0;
            for (var pkIndex = 0; pkIndex < pkArray.length; pkIndex++) {
			
                var pkItem = pkArray[pkIndex];
                var port = 1;
				
                for (businessVLAN = pkItem.beginBusinessVLAN; businessVLAN <= pkItem.endBusinessVLAN; businessVLAN++) {
                    var resultObject = new resultObj(pkItem, null, businessVLAN, port);

                    for (var fkIndex = 0; fkIndex < fkArray.length; fkIndex++) {
                        var fkItem = fkArray[fkIndex];
                        if (fkItem.businessVLAN == businessVLAN && fkItem.outerLayerVLAN == pkItem.outerLayerVLAN) {
                            resultObject.brasObject = fkItem;
							resultObject.username = fkItem.username;
                            break;
                        }
                    }

                    mergedResultArray[resultIndex++] = resultObject;
                    port++;
                }
            }

            //free data
			freeActiveXObject();
        }

        function searchFromMergedResultArray() {
			var searchResultArray = [];
			var resultIndex = 0;
			var parameters = getSearchingParameters();
			
            for (var index = 0; index < mergedResultArray.length; index++) {
				var item = mergedResultArray[index];
				
				if (item.username == (parameters.username != "" ? parameters.username : item.username) && 
					item.businessVLAN == (parameters.businessVLAN > 0 ? parameters.businessVLAN : item.businessVLAN) &&
					item.onuObject.outerLayerVLAN == (parameters.outerLayerVLAN > 0 ? parameters.outerLayerVLAN : item.onuObject.outerLayerVLAN) &&
					item.onuObject.beginBusinessVLAN <= (parameters.beginBusinessVLAN > 0 ? parameters.beginBusinessVLAN : item.onuObject.beginBusinessVLAN) &&
					item.onuObject.endBusinessVLAN >= (parameters.endBusinessVLAN > 0 ? parameters.endBusinessVLAN : item.onuObject.endBusinessVLAN)) {
			
					searchResultArray[resultIndex++] = item;
				}
			}
		
			renderHTML(searchResultArray);
			
			setMessage("");
			searchResultArray = [];
        }
		
		function getSearchingParameters() {
			this.username = document.getElementById("tbxUsername").value;
            this.businessVLAN = isNaN(parseInt(document.getElementById("tbxBusinessVLAN").value)) ? 0 : parseInt(document.getElementById("tbxBusinessVLAN").value);
            this.outerLayerVLAN = isNaN(parseInt(document.getElementById("tbxOuterLayerVLAN").value)) ? 0 : parseInt(document.getElementById("tbxOuterLayerVLAN").value);
            this.beginBusinessVLAN = isNaN(parseInt(document.getElementById("tbxBeginBusinessVLAN").value)) ? 0 : parseInt(document.getElementById("tbxBeginBusinessVLAN").value);
            this.endBusinessVLAN = isNaN(parseInt(document.getElementById("tbxEndBusinessVLAN").value)) ? 0 : parseInt(document.getElementById("tbxEndBusinessVLAN").value);
			
			return this;
		}
		
		function renderHTML(searchResultArray) {
			var htmlArray = new Array();
			var htmlContent = document.getElementById("htmlContent");
			htmlArray.push("<div style='text-align: center; width:100%;'><table align='center' id='contentTable'>");
			
			//Header
			htmlArray.push("<tr bgcolor='#86B8E1'>");
			
			htmlArray.push("<td>小区名</td>");
			htmlArray.push("<td>设备位置</td>");
			htmlArray.push("<td>设备类型</td>");
			htmlArray.push("<td>端口数</td>");
			htmlArray.push("<td>序列号</td>");
			htmlArray.push("<td>未命名</td>");
			htmlArray.push("<td>外层VLAN</td>");
			htmlArray.push("<td>管理VLAN</td>");
			htmlArray.push("<td>业务VLAN(始)</td>");
			htmlArray.push("<td>业务VlAN(终)</td>");
			htmlArray.push("<td>管理IP</td>");
			htmlArray.push("<td>掩码</td>");
			htmlArray.push("<td>网关</td>");
			htmlArray.push("<td>业务VLAN</td>");
			htmlArray.push("<td>端口号</td>");
			htmlArray.push("<td>用户名</td>");
			
			htmlArray.push("</tr>");
				
			//Body
			for (var index = 0; index < searchResultArray.length; index++) {
				var resultObject = searchResultArray[index];
				
				htmlArray.push("<tr>");
				
				htmlArray.push("<td>" + resultObject.onuObject.zoneName + "</td>");
				htmlArray.push("<td>" + resultObject.onuObject.devicePosition + "</td>");
				htmlArray.push("<td>" + resultObject.onuObject.deviceType + "</td>");
				htmlArray.push("<td>" + resultObject.onuObject.numberOfPorts + "</td>");
				htmlArray.push("<td>" + resultObject.onuObject.serialNumber + "</td>");
				htmlArray.push("<td>" + resultObject.onuObject.unnamed + "</td>");
				htmlArray.push("<td>" + resultObject.onuObject.outerLayerVLAN + "</td>");
				htmlArray.push("<td>" + resultObject.onuObject.manageVLAN + "</td>");
				htmlArray.push("<td>" + resultObject.onuObject.beginBusinessVLAN + "</td>");
				htmlArray.push("<td>" + resultObject.onuObject.endBusinessVLAN + "</td>");
				htmlArray.push("<td>" + resultObject.onuObject.manageIP + "</td>");
				htmlArray.push("<td>" + resultObject.onuObject.subnetMask + "</td>");
				htmlArray.push("<td>" + resultObject.onuObject.defaultGateway + "</td>");
				htmlArray.push("<td>" + resultObject.businessVLAN + "</td>");
				htmlArray.push("<td>" + resultObject.port + "</td>");
				htmlArray.push("<td>" + resultObject.username + "</td>");

				htmlArray.push("</tr>");
			}
			
			htmlArray.push("</table></div>");
			
			// Render top page
			htmlContent.innerHTML = htmlArray.join("");
		}

		function exportSearchResultToExcel() {		 
			var contentTable = document.getElementById('contentTable');  
			
			// init ActiveXObject object
			activeXObject = new ActiveXObject("Excel.Application");
			workBooks = activeXObject.Workbooks.Add();      
			outputSheet = workBooks.Worksheets(1);    
			
			var sel = document.body.createTextRange();   
			sel.moveToElementText(contentTable);     
			sel.select();    
			sel.execCommand("Copy");    
			outputSheet.Paste();           
			activeXObject.Visible = true;    
  
			try{  
				var fname = activeXObject.Application.GetSaveAsFilename("save.xls", "Excel Spreadsheets (*.xls), *.xls");  
				
				if(fname){  
					workBooks.SaveAs(fname);  
				}  
			}catch(e){  
				setMessage(e);  
			}finally{  
				
				//free data
				outputSheet = null;
				freeActiveXObject();
			}  
			
			setMessage("数据导出成功！！！");
        }
		
		function freeActiveXObject() {
			pkRowCount = 0;
			fkRowCount = 0;
			
			pkColCount = 0;
			fkColCount = 0;
		
		    pkArray = [];
            fkArray = [];
			
			pkSheet = null;
            fkSheet = null;
			
			workBooks.Close(savechanges=false);
			
			activeXObject.Quit();  
			activeXObject = null;  
			
			idTmr = window.setInterval("closeProcess();",1);
		}
		
        function closeProcess() {
            window.clearInterval(idTmr);
            CollectGarbage();
        }
		
		function sleep (second){  
			var startTime, endTimes, s;   
			var d = new Date();   
			
			startTime = d.getTime();   
			while(true){  
				d = new Date();  
				endTime = d.getTime();  
				s = (endTime - startTime) / 1000;  
				if (s >= second) { break; }
			}  
		}  
		
		/************************************************************/
		
        function brasObj(username, bras, businessVLAN, outerLayerVLAN) {
            //USERNAME
            this.username = username;
            //BRAS
            this.bras = bras;
            //业务VLAN
            this.businessVLAN = isNaN(parseInt(businessVLAN)) ? 0 : parseInt(businessVLAN);
            //外层LVAN
            this.outerLayerVLAN = isNaN(parseInt(outerLayerVLAN)) ? 0 : parseInt(outerLayerVLAN);
        }

        function onuObj(zoneName, devicePosition, deviceType, numberOfPorts, serialNumber, unnamed,
					outerLayerVLAN, manageVLAN, beginBusinessVLAN, endBusinessVLAN, manageIP, subnetMask, defaultGateway) {
            //小区名
            this.zoneName = zoneName;
            //设备位置
            this.devicePosition = devicePosition;
            //设备类型
            this.deviceType = deviceType;
            //端口数量
            this.numberOfPorts = isNaN(parseInt(numberOfPorts)) ? 0 : parseInt(numberOfPorts);
            //序列号
            this.serialNumber = serialNumber;
            //未命名
            this.unnamed = unnamed;
            //外层LVAN
            this.outerLayerVLAN = isNaN(parseInt(outerLayerVLAN)) ? 0 : parseInt(outerLayerVLAN);
            //管理LVAN
            this.manageVLAN = manageVLAN;
            //业务VLAN(始)
            this.beginBusinessVLAN = isNaN(parseInt(beginBusinessVLAN)) ? 0 : parseInt(beginBusinessVLAN);
            //业务VLAN(终)
            this.endBusinessVLAN = isNaN(parseInt(endBusinessVLAN)) ? 0 : parseInt(endBusinessVLAN);
            //管理IP
            this.manageIP = manageIP;
            //子网掩码
            this.subnetMask = subnetMask;
            //默认网关
            this.defaultGateway = defaultGateway;
        }

        function resultObj(onuObject, brasObject, businessVLAN, port) {
            //ONU object
            this.onuObject = onuObject;
            //BRAS Object
            this.brasObject = brasObject;
            //用户名
			this.username = "";
			//业务VLAN
            this.businessVLAN = isNaN(parseInt(businessVLAN)) ? 0 : parseInt(businessVLAN);
            //端口号
            this.port = isNaN(parseInt(port)) ? 0 : parseInt(port);
        }

    </script>
</body>
</html>
